﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.Proj5715.Process
{
    public partial class MissileArriveShow : System.Web.UI.Page
    {
        public string ItemCode
        {
            set
            {
                ViewState["ItemCode"] = value;
            }
            get
            {
                return ViewState["ItemCode"] != null ? ViewState["ItemCode"].ToString() : string.Empty;
            }
        }

        public string DistNumber
        {
            set
            {
                ViewState["DistNumber"] = value;
            }
            get
            {
                return ViewState["DistNumber"] != null ? ViewState["DistNumber"].ToString() : string.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.ItemCode = Request.QueryString["itemCode"];
                this.DistNumber = Request.QueryString["distNumber"];
                InitControlData();
            }
        }

        private void InitControlData()
        {
            string sql = @"SELECT TOP 1 * FROM dbo.OSRN WHERE ItemCode = @ItemCode AND DistNumber = @DistNumber ORDER BY SysNumber DESC";

            try
            {
                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ItemCode", DbType.String, this.ItemCode);
                database.AddInParameter(dbCommand, "@DistNumber", DbType.String, this.DistNumber);
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        txtItemCode.Text = reader["ItemCode"] != null ? reader["ItemCode"].ToString() : string.Empty;
                        txtItemName.Text = reader["ItemName"] != null ? reader["ItemName"].ToString() : string.Empty;
                        txtDistNumber.Text = reader["DistNumber"] != null ? reader["DistNumber"].ToString() : string.Empty;
                        txtInDate.Text = reader["InDate"] != null ? reader["InDate"].ToString() : string.Empty;
                        txtYccsj.Text = reader["U_yccsj"] != null ? reader["U_yccsj"].ToString() : string.Empty;
	                    txtTdsj.Text = reader["U_tdsj"] != null ? reader["U_tdsj"].ToString() : string.Empty;
	                    txtGfsj.Text = reader["U_gfsj"] != null ? reader["U_gfsj"].ToString() : string.Empty;
	                    txtCchj.Text = reader["U_cchj"] != null ? reader["U_cchj"].ToString() : string.Empty;
	                    txtYfsj.Text = reader["U_yfsj"] != null ? reader["U_yfsj"].ToString() : string.Empty;
	                    txtQfsj.Text = reader["U_qfsj"] != null ? reader["U_qfsj"].ToString() : string.Empty;
                        txtLifeTime.Text = reader["U_LifeTime"] != null ? reader["U_LifeTime"].ToString() : string.Empty;

                        string level = reader["U_Level"] != null ? reader["U_Level"].ToString() : string.Empty;
                        ListItem listItem = ddlLevel.Items.FindByValue(level);
                        if (listItem != null)
                        {
                            listItem.Selected = true;
                        }

                        string strInDate = txtInDate.Text;
                        string strLifeTime = txtLifeTime.Text;

                        DateTime inDate = DateTime.Now;
                        int lifeTime = 0;

                        if (DateTime.TryParse(strInDate, out inDate) && int.TryParse(strLifeTime, out lifeTime))
                        {
                            TimeSpan timeSpan = DateTime.Now.Subtract(inDate);
                            double temp = timeSpan.Days / 30.00;
                            double newLifeTime = lifeTime - temp;
                            if (newLifeTime < 0)
                            {
                                newLifeTime = 0;
                            }

                            txtLifeTime2.Text = Math.Round(newLifeTime, 2).ToString();
                        }                        
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        
    }
}